/*
 * #%L
 * SwiftUIBusinessLayer
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2011 - 2012 Linkwithweb
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */
/**
 * 
 */
package com.linkwithweb.products.swiftui.plugins;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author ashwin kumar
 * 
 */
public abstract class AbstractSwiftUIPlugin extends org.java.plugin.Plugin implements SwiftUIPlugin {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	protected boolean isLoaded = false;
	private String pluginName = "";

	/**
	 * @return the pluginName
	 */
	public String getPluginName() {
		return pluginName;
	}

	/**
	 * @param pluginName
	 *            the pluginName to set
	 */
	public void setPluginName(String pluginName) {
		this.pluginName = pluginName;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.linkwithweb.products.swiftui.plugins.SwiftUIPlugin#initPlugin(com
	 *      .linkwithweb.products.swiftui.plugins.SwiftUIPluginContext)
	 */
	@Override
	public void initPlugin(SwiftUIPluginContext pluginContext) {
		try {
			if (!isLoaded) {
				setPluginName(pluginContext.getPluginName());
				loadSQL(pluginContext);
			}
		} finally {
			isLoaded = true;
		}

	}

	/**
	 * Run the SQL attached so as to maintain data structure
	 * 
	 * @param pluginContext
	 * 
	 */
	private void loadSQL(SwiftUIPluginContext pluginContext) {
		SimpleJdbcTemplate simpleJdbcTemp = new SimpleJdbcTemplate(pluginContext.getPlatformService().getPlatformDAO()
				.getJdbcTemplate("jdbcTemplate").getDataSource());
		logger.info("Executing scripts for Plugin :" + pluginContext.getPluginName());

		// String script =
		// getClass().getClassLoader().getResource("/").getPath();
		String script = "sql/" + pluginContext.getPluginName() + "/setup.sql";
		logger.info("Executing script [" + script + "]");
		SimpleJdbcTestUtils.executeSqlScript(simpleJdbcTemp, new ClassPathResource(script), false);
	}

	/**
	 * @param pluginContext
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView loadPlugin(SwiftUIPluginContext pluginContext) {
		return pluginModelAndView("index", new HashMap());
	}

	/**
	 * @param pageName
	 * @return
	 */
	protected ModelAndView pluginModelAndView(String pageName, Map params) {
		return new ModelAndView("plugins/" + getPluginName() + "/" + pageName, params);
	}
	
	@Override
	protected void doStart() throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void doStop() throws Exception {
		// TODO Auto-generated method stub
		
	}


}
